package com.cpi.cornpp.version.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.monthsperiods.services.MonthsPeriodsService;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.form.VersionForm;
import com.cpi.cornpp.version.services.VersionService;

/**
 * Version Filter Search Action
 * 
 * @author Edgar Joao
 *
 */
public class VersionFilterSearchAction extends Action{
	/** Default forward */
	private static final String DEFAULT = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/**
	 * Metodo que realiza la busqueda de la version
	 */
	public ActionForward execute(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		int actualMonth = 0;
		VersionForm versionForm = null;
		MonthsPeriodsService monthPeriodService = null;
		VersionService versionService = null;
		List monthPeriodsView = null;
		List versionsView = null;		
		VersionService service = null;
		List listVersionViews = null;
		int orderField = 0;
		int orderType = 0;
		HttpSession session = null;				
						
		try{
			versionForm = (VersionForm) argForm;
			session = argRequest.getSession(false);					    
			monthPeriodService = new MonthsPeriodsService();
			versionService = new VersionService();
			
			listVersionViews = new ArrayList(0);
			service = new VersionService();
			
			actualMonth = monthPeriodService.actualMonthPeriod();
			monthPeriodsView = monthPeriodService.listOfMonthsPeriods();
			versionsView = versionService.list(0, true);
			
			argRequest.setAttribute("actualMonth", new Integer(actualMonth));
			argRequest.setAttribute("actualYear", new Integer(Calendar.getInstance().get(Calendar.YEAR)));
			argRequest.setAttribute("monthPeriods", monthPeriodsView);
			argRequest.setAttribute("versions", versionsView);			
			
			if(argForm != null){
				orderField 	= versionForm.getOrderField();
				orderType 	= versionForm.getOrderType();
			}
			
			listVersionViews = service.list(orderField,(1 == orderType) ? false : true);
			argRequest.setAttribute("listVersionViews", listVersionViews);
					
		} catch(Exception e){
			VersionException versionException=null;
			e.printStackTrace(System.out);
			versionException= new VersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}		
		return argMapping.findForward(DEFAULT);
	}
}
